<?php
/**
 * $Id$
 */

/**
 * 提示应用
 *
 * @package App
 */
class App_Notice extends Tinyask_App
{
	/**
	 * 提示框类型
	 *
	 * @var string
	 */
	public $noticeType = 'notice';

	/**
	 * 提示高亮
	 *
	 * @var string
	 */
	public $highlight;

	/**
	 * 执行函数
	 *
	 */
	public function execute()
	{

		if($this->request->getCookie('__tinyask_notice') !== null) {
			$this->noticeType = $this->request->getCookie('__tinyask_notice_type');
			$this->push($this->request->getcookie('__tinyask_notice'));
			$this->response->deleteCookie('__tinyask_notice', $this->app('App_Config')->siteUrl);
			$this->response->deleteCookie('tinyask_notice_type', $this->app('App_Config')->siteUrl);
		}

		if($this->request->getCookie('__typecho_notice_highlight') !== null) {
			$this->highlight = $this->request->getCookie('__tinyask_notice_highlight');
			$this->response->deleteCookie('__tinyask_notice_highlight', $this->app('App_Config')->siteUrl);
		}
	}

	/**
	 * 输出提示类型
	 *
	 */
	public function noticeType()
	{
		echo $this->noticeType;
	}

	/**
	 * 列表输出所有提示内容
	 *
	 * @param string $tag
	 */
	public function lists($tag = 'li')
	{
		foreach ($this->row as $row) {
			echo "<$tag>" . $row . "</$tag>";

		}
	}

	/**
	 * 输出相应提示字段
	 *
	 * @param string $name
	 * @param string $format
	 */
	public function display($name, $format = '%s')
	{
		echo empty($this->row[$name]) ? null : (strpos($format, '%s') === false ? $format : sprintf($format, $this->row[$name]));
	}

	/**
	 * 高亮相关元素
	 *
	 * @param string $id
	 */
	public function highlight($id)
	{
		$this->highlight = $id;
		$this->response->setCookie('__tinyask_notice_highlight', $id, $this->app('App_Config')->gmtTime + $this->app('App_Config')->timezone + 86400, $this->app('App_Config')->siteUrl);
	}

	/**
	 * 获取高亮的id
	 *
	 * @return integer
	 */
	public function getHighlightId()
	{
		return preg_match("/[0-9]+", $this->highlight, $matches) ? $matches[0] : 0;
	}

	/**
	 * 设置堆栈每一行的值
	 *
	 * @param string $name
	 * @param mixed $value
	 * @param string $type
	 */
	public function set($name, $value = null, $type = 'notice')
	{
		$notice = array();

		if(is_array($name)) {
			foreach ($name as $key => $row) {
				$notice[$key] = $row;
			}
		} else {
			if(empty($value)) {
				$notice[] = $name;
			} else {
				$notice[$name] = $value;
			}
		}

		$this->noticeType = $type;
		$this->push($notice);

		$this->response->setCookie('__tinyask_notice', $notice, $this->app('App_Config')->gmtTime + $this->app('App_Config')->timezone + 86400, $this->app('App_Config')->siteUrl);
		$this->response->setCookie('__tinyask_notice_type', $type, $this->app('App_Config')->gmtTime + $this->app('App_Config')->timezone + 86400, $this->app('App_Config')->siteUrl);
	}
}
